package com.example.jpa.controller;

import com.example.jpa.pojo.User;
import com.example.jpa.repository.UserRepository;
import org.aspectj.lang.annotation.AfterReturning;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Optional;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    UserRepository userRepository;

    @GetMapping("/{userId}")
    public User getUserById(@PathVariable Integer userId){

        Optional<User> byId = this.userRepository.findById(userId);

        return byId.get();
    }

    @GetMapping("/saveUser")
    public String saveUser(User user){
        this.userRepository.save(user);
        return "OK";
    }

}
